from flask import Flask,render_template
from flask import request
import hashlib
import string

app = Flask(__name__)


@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "GET":       # 判断请求方式是GET请求
        my_signature = request.args.get('signature')     # 获取携带的signature参数
        my_timestamp = request.args.get('timestamp')     # 获取携带的timestamp参数
        my_nonce = request.args.get('nonce')        # 获取携带的nonce参数
        my_echostr = request.args.get('echostr')         # 获取携带的echostr参数

        token = 'super'     # 一定要跟刚刚填写的token一致

        # 进行字典排序,这里很操蛋，列表中获取到的值有可能为NoneType 
        data = [token, "xx", "wef"]

        #data.sort()

        # 拼接成字符串
        temp = ''.join(data)

        
        s = hashlib.sha1()
        s.update(temp.encode("utf-8"))
        # 进行sha1加密
        mysignature = s.hexdigest()

        # 加密后的字符串可与signature对比，标识该请求来源于微信
        if my_signature == mysignature:
            return my_echostr
    return render_template('index.html')   


if __name__ == "__main__":
    app.run(host="127.0.0.1", port=80, debug=True)
